package com.scnu.yxp.travelapp.homepage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.scnu.yxp.travelapp.R;
import com.scnu.yxp.travelapp.adapter.HomePageListViewAdapter;
import com.scnu.yxp.travelapp.view.MyListView;
import com.scnu.yxp.travelapp.view.MyListView.OnFooterRefreshListener;
import com.scnu.yxp.travelapp.view.MyListView.OnHeaderRefreshListener;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

public class LocationSearchActivity extends Activity {
	//按钮
	private ImageView backBtn;
	private ImageView labelBtn;
	private MyListView listView;
	//设置适配器
	private HomePageListViewAdapter adapter;
	//设置弹出窗口
	private PopupWindow labelMenu;
	private boolean isMenuShow = false;
	private ImageView landscape_label, food_label, life_label;
	//回到顶部按钮
	private ImageButton upBtn;
	private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
	private boolean hasResult = true;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_location_search);
		backBtn = (ImageView)findViewById(R.id.back_btn);
		listView = (MyListView)findViewById(R.id.listView);
		upBtn = (ImageButton)findViewById(R.id.up_btn);
		for(int i = 0; i < 4; i++)
		{
			Map<String, String> listItem = new HashMap<String, String>();
			listItem.put(i+"", i+"");
			list.add(listItem);
		}
		adapter = new HomePageListViewAdapter(this, list);
		listView.setAdapter(adapter);
		backBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		upBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//回到顶部,listview需要用setSelection,ScrollView要用scrollto
				listView.setSelection(0);
				upBtn.setVisibility(View.GONE);
			}
		});
		listView.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView arg0, int arg1) {
				
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				if(firstVisibleItem == 2)
				{
					upBtn.setVisibility(View.VISIBLE);
				}else if(firstVisibleItem == 1)
				{
					upBtn.setVisibility(View.GONE);
				}
			}
		});
		listView.setOnHeaderRefreshListener(new OnHeaderRefreshListener() {
			
			@Override
			public void onRefresh() {
				//更新内容
				Log.i("refresh","refresu_header");
				Map<String, String> listItem = new HashMap<String, String>();
				listItem.put("1", "0");
				list.add(1, listItem);
				handler.sendEmptyMessage(0x110);
			}
		});
		listView.setOnFooterRefreshListener(new OnFooterRefreshListener() {
			
			@Override
			public boolean onRefresh() {
				Log.i("refresh","refresu_footer");
				//这里模拟有误数据返回
				if(hasResult)
				{
					//加载内容
					Map<String, String> listItem = new HashMap<String, String>();
					listItem.put("1", "0");
					list.add(listItem);
					handler.sendEmptyMessage(0x110);
					hasResult = false;
					//有返回数据，返回真
					return true;
				}else{
					//无返回值的情况，则没有数据，返回假
					return false;
				}
			}
		});
	}
	
	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			if(msg.what == 0x110)
			{
				//更新listview的内容
				adapter.notifyDataSetChanged();
			}
		}};
}
